package SwingView.Assignment;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import SwingController.CmsPresenter;
import SwingModel.CmsAssignment;
import SwingModel.CmsQuiz;
import SwingView.CmsContentPanelModel;
import SwingView.Quiz.ALQuizTake;
import SwingView.Quiz.ALQuizViewResult;
import SwingView.Quiz.CmsCPQuizMainStudent.QuizEntryRow;

public class CmsCPAsgmMainStudent extends CmsContentPanelModel{

	private JLabel jlTitle;
	private JPanel jpContent;
	
	public CmsCPAsgmMainStudent(){
		//instantiate People title
		jlTitle = new JLabel("Assignment");
		jlTitle.setFont(jlTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 8, 10, 8));
		
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(650,40));
		jp.setOpaque(false);
		jp.setLayout( new FlowLayout(FlowLayout.CENTER));
		jp.setBorder(BorderFactory.createEmptyBorder());
		jp.add(jlTitle);
		
		//content panel
		jpContent = new JPanel();
		jpContent.setLayout(new GridBagLayout());
		jpContent.setOpaque(false);

		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);

		//set up student people panel and add components
		setLayout(new BorderLayout());
		add(jp,BorderLayout.NORTH);//Add title People
		add(scroll,BorderLayout.WEST);//Add People List
	}
	
	public void displayListOfAssignment(List<CmsAssignment> listOfAsgm){
		jpContent.removeAll();
		jpContent.revalidate();
		jpContent.repaint();
		int i = 0;
		for(CmsAssignment asgm : listOfAsgm){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			c.insets = new Insets(0, 0, 8, 0);
			jpContent.add(new AsgmEntryRow(asgm),c);
			i++;
		}
		
		//empty space at bottom - used to fill the bottom of the page
		//when the list is too short
		JPanel p = new JPanel();
		p.setOpaque(false);
		p.setBackground(Color.WHITE);
		GridBagConstraints c_p = new GridBagConstraints();
		c_p.gridy = i;
		c_p.fill = GridBagConstraints.VERTICAL;
		c_p.weightx=1.0;
		c_p.weighty=1.0;
		jpContent.add(p,c_p);
		
		jpContent.revalidate();
		jpContent.repaint();
	}
	
	public class AsgmEntryRow extends JPanel{
		
		
		final static private int ROW_WIDTH = 550;
		final static private int ROW_HEIGHT = 100;
		
		final static private int DATE_NAME_COLUMN_SIZE = 100;
		private JButton viewButton;
		private CmsAssignment asgm;
		
		public AsgmEntryRow(final CmsAssignment asgm){
			this.asgm = asgm;
			
			JLabel jlTitle;
			JLabel postedDate; //avai date
			JLabel pdTitle;
			
			
			//Set up file name label
			String quizTitle = asgm.getAsgmTitle();
			jlTitle = new JLabel(quizTitle);
			jlTitle.setFont(jlTitle.getFont().deriveFont(15.0f).deriveFont(Font.BOLD));
			jlTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 5, 5));
			
			//Set up the "Available date" title label
			pdTitle = new JLabel("Posted Date: ");
			pdTitle.setFont(new Font("Lucida Grande", Font.BOLD, 10));
			
			//set up avai date Label
			postedDate = new JLabel(CmsPresenter.sdf.format(new Date(asgm.getCreatedDate().getTime())));
			postedDate.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
			postedDate.setForeground(new Color(40,40,40));
			
			//set up view button
			viewButton = new JButton ("View/Submit");
			viewButton.setFont(viewButton.getFont().deriveFont(12.0f));
			viewButton.setForeground(Color.BLUE);
			viewButton.setOpaque(false);
			viewButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			viewButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			
			//Set up the panel
			setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setBackground(Color.WHITE);
			setBorder(BorderFactory.createLineBorder(new Color(170,170,170), 1));
			setOpaque(true);
			setLayout(new GridBagLayout());
			
			//add the Title
			GridBagConstraints cf = new GridBagConstraints();
			cf.fill = GridBagConstraints.HORIZONTAL;
			cf.anchor = GridBagConstraints.FIRST_LINE_START;
			cf.gridwidth = 4;
			add(jlTitle,cf);
			
			//add "Posted date" title:
			GridBagConstraints cp = new GridBagConstraints();
			cp.insets = new Insets(0,10,5,0);
			cp.gridx = 0;
			cp.gridy = 1;
			cp.anchor = GridBagConstraints.LINE_START;
			add(pdTitle,cp);
			
			//add the posted date
			GridBagConstraints cd = new GridBagConstraints();
			cd.insets = new Insets(0,10,5,0);
			cd.gridx = 1;
			cd.gridy = 1;
			cd.anchor = GridBagConstraints.LINE_START;
			add(postedDate,cd);
			
			//add due date
			JLabel lblDueDate = new JLabel("Due Date: ");
			lblDueDate.setFont(new Font("Lucida Grande", Font.BOLD, 10));
			GridBagConstraints gbc_lblDueDate = new GridBagConstraints();
			gbc_lblDueDate.anchor = GridBagConstraints.LINE_START;
			gbc_lblDueDate.insets = new Insets(0, 10, 5, 0);
			gbc_lblDueDate.gridx = 0;
			gbc_lblDueDate.gridy = 2;
			add(lblDueDate, gbc_lblDueDate);
			
			JLabel jlbDueDate = new JLabel(CmsPresenter.sdf.format(new Date(asgm.getDueDate().getTime())));
			jlbDueDate.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
			GridBagConstraints gbc_jlbDueDate = new GridBagConstraints();
			gbc_jlbDueDate.anchor = GridBagConstraints.LINE_START;
			gbc_jlbDueDate.insets = new Insets(0, 10, 5, 0);
			gbc_jlbDueDate.gridx = 1;
			gbc_jlbDueDate.gridy = 2;
			add(jlbDueDate, gbc_jlbDueDate);
			
			//add Weight
			JLabel lblWeight = new JLabel("Percent Weight: ");
			lblWeight.setFont(new Font("Lucida Grande", Font.BOLD, 10));
			GridBagConstraints c = new GridBagConstraints();
			c.anchor = GridBagConstraints.LINE_END;
			c.insets = new Insets(0, 10, 5, 0);
			c.weightx = 0.2;
			c.gridx = 2;
			c.gridy = 1;
			add(lblWeight, c);
			
			JLabel jlbWeight = new JLabel("" + asgm.getPercentWeight() + "%");
			jlbWeight.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
			c = new GridBagConstraints();
			c.anchor = GridBagConstraints.LINE_START;
			c.insets = new Insets(0, 10, 5, 0);
			c.weightx = 0.8;
			c.gridx = 3;
			c.gridy = 1;
			add(jlbWeight, c);
			
			
			
			//add the take button
			GridBagConstraints cb = new GridBagConstraints();
			cb.gridx = 3;
			cb.gridy = 3;
			cb.weightx = 1.0;
			cb.anchor = GridBagConstraints.LAST_LINE_END;
			add(viewButton,cb);


			
			//Add View AL:
			viewButton.addActionListener(new ALAssignmentStudentView(asgm));
			
		}
	}
}
